const { Router } = require("express");
const wxorders = require("../model/wxorders");

const router = new Router();

//加入购物车
router.post("/add", async (req, res) => {
  const { useid, id, state, name, unit_price, Price, unit, num, img } =
    req.body;
  let result1 = await wxorders.find({ id, useid });
  console.log(result1);
  if (result1.length == 0) {
    await wxorders.create({
      useid,
      id,
      state,
      name,
      unit_price,
      Price,
      unit,
      num,
      img,
    });
    res.json({ msg: "添加成功" });
  } else {
    await wxorders.updateOne({ id, useid }, { num });
    res.json({ msg: "修改成功" });
  }
});

//查询商品是否已经付款
router.post("/search", async (req, res) => {
  const { id, openid } = req.body;
  let result = await wxorders.find({ id: id, useid: openid });
  console.log(result);
  if (result.length != 0 && result[0].state == 0) {
    res.json({ result });
    return;
  }
  res.json({});
});

router.get("/que", async (req, res) => {
  const { useid } = req.query;
  const data = await wxorders.find({ useid });
  res.json({ msg: "添加成功", data });
});

module.exports = router;

/* 

  id: {
   type:String,
   required:true
  },
 state:{
    type:Number,
    default:1
 },
  name: {
    type: String,
    required: true,
  },
  unit_price: {
    type: Number,
    required: true,
  },
  Price:{
    typeof:Number,
    required:true
  },
  unit:{
    type:String,
    required:true
  },
  num:{
    type:String,
    required:true
  },
  img:{
    type:String,
    required:true
  }
*/
